﻿using ChinaUnicom.Controls;
using ChinaUnicom.Models;
using Microsoft.Phone.Controls;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Navigation;

namespace ChinaUnicom.Views
{
    public class SignedInfoModel : INotifyPropertyChanged
    {
        public SignedInfoModel(Account account)
        {
            Client = account.ClientName;
            Mobile = account.Mobile;
            LastSigned = account.LastSigned;
            LastUpdated = account.LastUpdated;
        }

        string client;

        public string Client
        {
            get { return client; }
            set
            {
                if (value != client)
                {
                    client = value;
                    NotifyPropertyChanged("Client");
                }
            }
        }
        
        string mobile;

        public string Mobile
        {
            get { return mobile; }
            set
            {
                if (value != this.mobile)
                {
                    this.mobile = value;
                    NotifyPropertyChanged("Mobile");
                }
            }
        }

        DateTime lastUpdated;

        public DateTime LastUpdated
        {
            get { return lastUpdated; }
            set
            {
                if (value != lastUpdated)
                {
                    lastUpdated = value;
                    NotifyPropertyChanged("LastUpdated");
                }
            }
        }

        DateTime lastSigned;

        public DateTime LastSigned
        {
            get { return lastSigned; }
            set
            {
                if (value != lastSigned)
                {
                    lastSigned = value;
                    NotifyPropertyChanged("LastSigned");
                }
            }
        }
        
        public event PropertyChangedEventHandler PropertyChanged;

        void NotifyPropertyChanged(string property)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(property));
            }
        }
    }

    public partial class SignedInfoPage : PhoneApplicationPage
    {
        public SignedInfoPage()
        {
            InitializeComponent();

            Loaded += OnPageLoaded;
        }

        void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            DataContext = new SignedInfoModel(Context.Current.Account);
        }

        private void OnLogoutClick(object sender, EventArgs e)
        {
            if (Message.Ask("确定要退出登录吗？", "温馨提示"))
            {
                // clear befor login.
                Authentication.Current.Logout();
                Context.Current.Reset();

                try
                {
                    NavigationService.RemoveBackEntry();
                }
                catch { }

                NavigationService.GoBack();
            }
        }

        private void OnBackClick(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}